var id = location.href.split('?')[1].split('=')[1]
var pageBox = document.getElementsByClassName('page_box')[0]
var pageTitle = pageBox.getElementsByClassName('page_title')[0];
var pageContent = pageBox.getElementsByClassName('page_content')[0];
var pageTime = pageBox.getElementsByClassName('page_time')[0];
var pageClassify = pageBox.getElementsByClassName('page_classify')[0]
var pageCount=pageBox.getElementsByClassName('page_count')[0]

var commendListUl = document.getElementsByClassName('commend_list_ul')[0];

var inputCommend = document.getElementsByClassName('input_commend')[0];
var submitBtn = document.getElementsByClassName('submit')[0]

var goLastBtn=document.getElementsByClassName('go-last')[0]
//填充文章内容
function renderOneArticle(e) {
    e = JSON.parse(e)[0]
    // console.log(e)
    document.title = e.title
    pageTitle.innerHTML = e.title
    pageContent.innerHTML = e.content
    pageClassify.innerHTML = e.classify
    pageTime.innerHTML = e.time
    pageCount.innerHTML=e.count
}
//填充评论
function renderCommendData(e) {
    // console.log(e)
    commendListUl.innerHTML=''
    e = JSON.parse(e)
    e.forEach((item) => {
        var li = document.createElement('li')
        li.className = 'commend_item'
        commendListUl.appendChild(li)
        var span1 = document.createElement('span')
        var span2 = document.createElement('span')
        span1.innerText = item.commend
        span2.innerText = item.date
        li.appendChild(span1)
        li.appendChild(span2)
    })
}
//提交评论
submitBtn.addEventListener('click', function () {
    var value = inputCommend.value
    if(value!=''){
        myAjax('POST', '/submitComment', { commends: value, id: id }).then((data) => {
            alert(data)
        })
        getData()
        inputCommend.value = ''
    }else{
        alert('评论不可为空！')
    }
})
//渲染浏览量
function renderBrowseCount(e){
    console.log(11)
}
//给返回上一步按钮添加监听器
goLastBtn.addEventListener('click',function(){
    location.href='/'
})
async function getData() {
    var oneArticleData = await myAjax('get', '/getOneArticle?id=' + id, null)
    renderOneArticle(oneArticleData)
    var commendData = await myAjax('get', '/getCommend?id=' + id, null)
    renderCommendData(commendData)
}

getData()

